﻿/*
 Copyright © BlazeApps 2006.  All rights reserved.
 Visit www.BlazeApps.com for more information about us.
 */
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BA.Validators
{
	public class RadioButtonDependantControlValidator : BaseValidator
	{
		#region " Members, Properties, Enums "

		public enum TargetControlType
		{
			TextBox = 0
		}

		private string m_ValueToCheck = String.Empty;
		private string m_TargetControlID = String.Empty;
		private TargetControlType m_TargetControlTypeSelection;

		public string ValueToCheck
		{
			get
			{
				return m_ValueToCheck;
			}
			set
			{
				m_ValueToCheck = value;
			}
		}

		public string TargetControlID
		{
			get
			{
				return m_TargetControlID;
			}
			set
			{
				m_TargetControlID = value;
			}
		}

		public TargetControlType TargetControlTypeSelection
		{
			get
			{
				return m_TargetControlTypeSelection;
			}
			set
			{
				m_TargetControlTypeSelection = value;
			}
		}

		#endregion

		protected override bool ControlPropertiesValid()
		{
			RadioButtonList source_rbl = ((RadioButtonList)FindControl(ControlToValidate));

			if (TargetControlTypeSelection == TargetControlType.TextBox)
			{
				TextBox target_tb = ((TextBox)FindControl(TargetControlID));

				foreach (ListItem li in source_rbl.Items)
				{
					if (li.Selected)
					{
						if (source_rbl.SelectedItem.Text.Trim().ToUpper().Equals(ValueToCheck.Trim().ToUpper()))
						{
							if (String.IsNullOrEmpty(target_tb.Text))
							{
								return false;
							}
						}
					}
				}
			}

			return true;
		}

		protected override bool EvaluateIsValid()
		{
			return CheckDependentControl();
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (EnableClientScript)
			{
				this.ClientScript();
			}
		}

		private void ClientScript()
		{
			StringBuilder sb_Script = new StringBuilder();
			BuildRadioButtonListClientScript(sb_Script);
		}

		private void BuildRadioButtonListClientScript(StringBuilder sb_Script)
		{
			RadioButtonList source_rbl = ((RadioButtonList)FindControl(ControlToValidate));

			if (TargetControlTypeSelection == TargetControlType.TextBox)
			{
				TextBox target_tb = ((TextBox)FindControl(TargetControlID));

				sb_Script.Append("<script language=\"javascript\">");
				sb_Script.Append("\r");
				sb_Script.Append("function verify_radio_childcontrol() ");
				sb_Script.Append("\r");
				sb_Script.Append("  { ");
				sb_Script.Append("\r");
				sb_Script.Append(String.Format("      var objRBL = document.getElementById('{0}');", source_rbl.ClientID));
				sb_Script.Append("\r");
				sb_Script.Append(String.Format("      var objTXT = document.getElementById('{0}');", target_tb.ClientID));
				sb_Script.Append("\r");
				sb_Script.Append("      var colRadioButtons = objRBL.getElementsByTagName(\"INPUT\");");
				sb_Script.Append("\r");
				sb_Script.Append("      var colValues = objRBL.getElementsByTagName(\"LABEL\");");
				sb_Script.Append("\r");
				sb_Script.Append("      if ( colRadioButtons != null ) ");
				sb_Script.Append("\r");
				sb_Script.Append("      { ");
				sb_Script.Append("\r");
				sb_Script.Append("          for ( i = 0; i < colRadioButtons.length; i++ ) ");
				sb_Script.Append("\r");
				sb_Script.Append("          { ");
				sb_Script.Append("\r");
				sb_Script.Append("               if ( colRadioButtons[i].checked ) ");
				sb_Script.Append("\r");
				sb_Script.Append("               { ");
				sb_Script.Append("\r");
				sb_Script.Append(String.Format("                   if(colValues[i].innerText.toUpperCase() == '{0}')", ValueToCheck.ToUpper()));
				sb_Script.Append("\r");
				sb_Script.Append("                   {");
				sb_Script.Append("\r");
				sb_Script.Append("                       if (objTXT == null) ");
				sb_Script.Append("\r");
				sb_Script.Append("                       { ");
				sb_Script.Append("\r");
				sb_Script.Append("                           return false; ");
				sb_Script.Append("\r");
				sb_Script.Append("                       } ");
				sb_Script.Append("\r");
				sb_Script.Append("                       else if(objTXT.value == '') ");
				sb_Script.Append("\r");
				sb_Script.Append("                       { ");
				sb_Script.Append("\r");
				sb_Script.Append("                           return false; ");
				sb_Script.Append("\r");
				sb_Script.Append("                       } ");
				sb_Script.Append("\r");
				sb_Script.Append("                       else ");
				sb_Script.Append("\r");
				sb_Script.Append("                       { ");
				sb_Script.Append("\r");
				sb_Script.Append("                           return true; ");
				sb_Script.Append("\r");
				sb_Script.Append("                       } ");
				sb_Script.Append("\r");
				sb_Script.Append("                   }");
				sb_Script.Append("\r");
				sb_Script.Append("               }");
				sb_Script.Append("\r");
				sb_Script.Append(String.Format("               if ( !colRadioButtons[i].checked && colValues[i].innerText.toUpperCase() == '{0}') ", ValueToCheck.ToUpper()));
				sb_Script.Append("\r");
				sb_Script.Append("              {");
				sb_Script.Append("\r");
				sb_Script.Append("                  objTXT.value = ''");
				sb_Script.Append("\r");
				sb_Script.Append("              }");
				sb_Script.Append("\r");
				sb_Script.Append("          }");
				sb_Script.Append("\r");
				sb_Script.Append("          return true;");
				sb_Script.Append("\r");
				sb_Script.Append("      }");
				sb_Script.Append("\r");
				sb_Script.Append("  }");
				sb_Script.Append("\r");
				sb_Script.Append("</script>");
				Page.ClientScript.RegisterClientScriptBlock(GetType(), "CustomValidator_RadioButtonListDependantControl", sb_Script.ToString());
				Page.ClientScript.RegisterExpandoAttribute(ClientID, "evaluationfunction", "verify_radio_childcontrol");
			}
		}

		protected bool CheckDependentControl()
		{
			RadioButtonList source_rbl = ((RadioButtonList)FindControl(ControlToValidate));

			if (TargetControlTypeSelection == TargetControlType.TextBox)
			{
				TextBox target_tb = ((TextBox)FindControl(TargetControlID));

				foreach (ListItem li in source_rbl.Items)
				{
					if (li.Selected)
					{
						if (source_rbl.SelectedItem.Text.Trim().ToUpper().Equals(ValueToCheck.Trim().ToUpper()))
						{
							if (String.IsNullOrEmpty(target_tb.Text))
							{
								return false;
							}
						}
					}
				}
			}

			return true;
		}
	}
}
